clear *

use "${root}/data/processed/final_sample.dta", clear

drop if irregular == 1

merge 1:1 mun_code year using "${root}/data/processed/demographic_dataset.dta"


* gen left mayor dummy
gen left_mayor=.
replace left_mayor=1 if margin_mayor_left>=0 & margin_mayor_left!=.
replace left_mayor=0 if margin_mayor_left<0 & margin_mayor_left!=.

* more manageable name for labor force participation rate and education variables
rename econ_active_workforce lfp
gen high_educ=bachelor_perc
gen low_educ=illiteracy_perc 

* gen log of population
gen ln_pop = ln(mun_pop)*100

* gen log of BF
gen ln_bf = ln(monthly_avg_households_pc_bf)*100
gen ln_bf_value = ln(total_annual_value_pc_bf)*100

*gen log of average earnings
label var avg_earnings "Average earnings (2016 constant values)"
label var median_earnings "Median earnings (2016 constant values)"
gen ln_earnings=ln(median_earnings)*100


*gen amendments as share of total revenues
gen tot_rev_sum2y = tot_rev_cp_t_0 + tot_rev_cp_t_minus1
gen amend_exec_sum2y = amendment_executed_cp_t_0 + amendment_executed_cp_t_minus1
gen amend_auth_sum2y = amendment_authorized_cp_t_0 + amendment_authorized_cp_t_minus1

gen amend_exec_share2y = amend_exec_sum2y/tot_rev_sum2y*100
gen amend_auth_share2y = amend_auth_sum2y/tot_rev_sum2y*100

local covariates ln_pop urb_perc white_perc ln_earnings high_educ low_educ lfp ln_bf ln_bf_value amend_exec_share2y amend_auth_share2y north northeast southeast south midwest
local geo_covariates north northeast southeast south midwest
local nongeo_covariates ln_pop urb_perc white_perc ln_earnings high_educ low_educ lfp ln_bf ln_bf_value amend_exec_share2y amend_auth_share2y

* filter out FE
foreach covariate of local nongeo_covariates {
	qui areg `covariate' yy*, absorb(mun_code)
	qui predict res_`covariate', res
	}

foreach covariate of local geo_covariates {
	gen res_`covariate'=`covariate'
	}
	
* estimations using all observations
foreach covariate of local covariates {
	reg `covariate' rank_1_mayor_left, vce(cluster mun_code)
	local `covariate'_all: di %9.2f _b[rank_1_mayor_left]
	local se_`covariate'_all: di %9.2f _se[rank_1_mayor_left]
	
	local t_`covariate'_all = _b[rank_1_mayor_left]/_se[rank_1_mayor_left]
	local pv_`covariate'_all = 2*ttail(e(df_r),abs(`t_`covariate'_all'))
	dis `pv_`covariate'_all'
	
	if `pv_`covariate'_all'>0.10 {
		* local stars_`covariate'_all
	}        

	if `pv_`covariate'_all'<=0.10 {
		* local stars_`covariate'_all "$^{*}$"
	}    

	if `pv_`covariate'_all'<=0.05 {
		* local stars_`covariate'_all "$^{**}$"
	}    

	if `pv_`covariate'_all'<=0.01 {
		* local stars_`covariate'_all "$^{***}$"
	}	

	
	}
local n_all = (e(N))
 	
* using a distance of +/- 40
foreach covariate of local covariates {
	reg `covariate' rank_1_mayor_left if abs(margin_mayor_left)<=40 & margin_mayor_left!=., vce(cluster mun_code)
	local `covariate'_40: di %9.2f _b[rank_1_mayor_left]
	local se_`covariate'_40: di %9.2f _se[rank_1_mayor_left]
	
	local t_`covariate'_40 = _b[rank_1_mayor_left]/_se[rank_1_mayor_left]
	local pv_`covariate'_40 = 2*ttail(e(df_r),abs(`t_`covariate'_40'))
	dis `pv_`covariate'_40'
	
	if `pv_`covariate'_40'>0.10 {
		* local stars_`covariate'_40
	}        

	if `pv_`covariate'_40'<=0.10 {
		* local stars_`covariate'_40 "$^{*}$"
	}    

	if `pv_`covariate'_40'<=0.05 {
		* local stars_`covariate'_40 "$^{**}$"
	}    

	if `pv_`covariate'_40'<=0.01 {
		* local stars_`covariate'_40 "$^{***}$"
	}	
	
	}
local n_40 = e(N)
	
* using a distance of +/- 10 
foreach covariate of local covariates {
	reg `covariate' rank_1_mayor_left if abs(margin_mayor_left)<=10 & margin_mayor_left!=., vce(cluster mun_code)
	local `covariate'_10: di %9.2f _b[rank_1_mayor_left]
	local se_`covariate'_10: di %9.2f _se[rank_1_mayor_left]
	
	local t_`covariate'_10 = _b[rank_1_mayor_left]/_se[rank_1_mayor_left]
	local pv_`covariate'_10 = 2*ttail(e(df_r),abs(`t_`covariate'_10'))
	dis `pv_`covariate'_10'
	
	if `pv_`covariate'_10'>0.10 {
		* local stars_`covariate'_10
	}        

	if `pv_`covariate'_10'<=0.10 {
		* local stars_`covariate'_10 "$^{*}$"
	}    

	if `pv_`covariate'_10'<=0.05 {
		* local stars_`covariate'_10 "$^{**}$"
	}    

	if `pv_`covariate'_10'<=0.01 {
		* local stars_`covariate'_10 "$^{***}$"
	}	
	
	}
local n_10 = e(N)

* using a distance of +/- 5
foreach covariate of local covariates {
	reg `covariate' rank_1_mayor_left if abs(margin_mayor_left)<=5 & margin_mayor_left!=., vce(cluster mun_code)
	local `covariate'_5: di %9.2f _b[rank_1_mayor_left]
	local se_`covariate'_5: di %9.2f _se[rank_1_mayor_left]
	
	local t_`covariate'_5 = _b[rank_1_mayor_left]/_se[rank_1_mayor_left]
	local pv_`covariate'_5 = 2*ttail(e(df_r),abs(`t_`covariate'_5'))
	dis `pv_`covariate'_5'
	
	if `pv_`covariate'_5'>0.10 {
		* local stars_`covariate'_5
	}        

	if `pv_`covariate'_5'<=0.10 {
		* local stars_`covariate'_5 "$^{*}$"
	}    

	if `pv_`covariate'_5'<=0.05 {
		* local stars_`covariate'_5 "$^{**}$"
	}    

	if `pv_`covariate'_5'<=0.01 {
		* local stars_`covariate'_5 "$^{***}$"
	}	
	
	}
local n_5 = e(N)

* using our preferred RD specification

* Keep only the baseline final sample
keep if baseline_sample == 1

foreach covariate of local geo_covariates {
	rdrobust `covariate' margin_mayor_left, vce(cluster mun_code) all
	local `covariate'_rd : di %9.2f (e(tau_bc))
	local se_`covariate'_rd : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_rd : di %9.2f (e(pv_rb))	
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_rd
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_rd "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_rd "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_rd "$^{***}$"
		}
	
	}
local n_rd = (e(N))
local eff_rd= e(N_h_l) + e(N_h_r)
	
foreach covariate of local nongeo_covariates {
	rdrobust res_`covariate' margin_mayor_left, vce(cluster mun_code) all
	local `covariate'_rd : di %9.2f (e(tau_bc))
	local se_`covariate'_rd : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_rd : di %9.2f (e(pv_rb))
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_rd
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_rd "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_rd "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_rd "$^{***}$"
		}
	
	}




* write table
texdoc init "${root}/results/tables/covariate_balance_table.tex", replace force

tex \caption{Difference in municipality characteristics between left and non-left mayors, by left margin of victory}

tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule
tex   										&       All    								&      +/- 40   						 &    +/- 10  								&   +/- 5   							& baseline RD      						\\
*tex   							   			&        (1)   								&      (2)      						 &       (3)  								&    (4)   								& (5)       							\\
tex \midrule

tex \multicolumn{6}{l}{Labor market and demographic covariates} \\
tex \midrule
tex log(Median earnings) $\times 100$       & `ln_earnings_all'`stars_ln_earnings_all' 	& `ln_earnings_40'`stars_ln_earnings_40' & `ln_earnings_10'`stars_ln_earnings_10' 	& `ln_earnings_5'`stars_ln_earnings_5' 	& `ln_earnings_rd'`stars_ln_earnings_rd'    \\
tex 				            			& (`se_ln_earnings_all') 					& (`se_ln_earnings_40') 				 & (`se_ln_earnings_10') 					& (`se_ln_earnings_5')  				& (`se_ln_earnings_rd') 					\\
tex Labor force participation          		& `lfp_all'`stars_lfp_all' 					& `lfp_40'`stars_lfp_40'  				 & `lfp_10'`stars_lfp_10' 					& `lfp_5'`stars_lfp_5' 					& `lfp_rd'`stars_lfp_rd'      				\\
tex  				            			& (`se_lfp_all') 							& (`se_lfp_40') 						 & (`se_lfp_10') 							& (`se_lfp_5')  						& (`se_lfp_rd') 							\\
tex log(Population) $\times 100$         	& `ln_pop_all'`stars_ln_pop_all'			& `ln_pop_40'`stars_ln_pop_40' 			 & `ln_pop_10'`stars_ln_pop_10' 			& `ln_pop_5'`stars_ln_pop_5' 			& `ln_pop_rd'`stars_ln_pop_rd'      		\\
tex                     					& (`se_ln_pop_all') 						& (`se_ln_pop_40') 						 & (`se_ln_pop_10') 						& (`se_ln_pop_5')  						& (`se_ln_pop_rd') 							\\
tex \% Urban          						& `urb_perc_all'`stars_urb_perc_all'		& `urb_perc_40'`stars_urb_perc_40' 		 & `urb_perc_10'`stars_urb_perc_10' 		& `urb_perc_5'`stars_urb_perc_5' 		& `urb_perc_rd'`stars_urb_perc_rd'      	\\
tex                     					& (`se_urb_perc_all') 						& (`se_urb_perc_40') 					 & (`se_urb_perc_10') 						& (`se_urb_perc_5')  					& (`se_urb_perc_rd') 						\\
tex \% White          						& `white_perc_all'`stars_white_perc_all' 	& `white_perc_40'`stars_white_perc_40' 	 & `white_perc_10'`stars_white_perc_10' 	& `white_perc_5'`stars_white_perc_5' 	& `white_perc_rd'`stars_white_perc_rd'    	\\
tex                     					& (`se_white_perc_all') 					& (`se_white_perc_40') 					 & (`se_white_perc_10') 					& (`se_white_perc_5')  					& (`se_white_perc_rd') 						\\
tex \% Higher education        				& `high_educ_all'`stars_high_educ_all' 		& `high_educ_40'`stars_high_educ_40'  	 & `high_educ_10'`stars_high_educ_10' 		& `high_educ_5'`stars_high_educ_5' 		& `high_educ_rd'`stars_high_educ_rd'      	\\
tex 				            			& (`se_high_educ_all') 						& (`se_high_educ_40') 					 & (`se_high_educ_10') 						& (`se_high_educ_5')  					& (`se_high_educ_rd') 						\\
tex \% Illiterate        					& `low_educ_all'`stars_low_educ_all' 		& `low_educ_40'`stars_low_educ_40'  	 & `low_educ_10'`stars_low_educ_10' 		& `low_educ_5'`stars_low_educ_5' 		& `low_educ_rd'`stars_low_educ_rd'      	\\
tex 				           				& (`se_low_educ_all') 						& (`se_low_educ_40') 					 & (`se_low_educ_10') 						& (`se_low_educ_5')  					& (`se_low_educ_rd') 						\\

tex \midrule
tex \multicolumn{6}{l}{Geographic indicators} \\
tex \midrule
tex North 			         				& `north_all'`stars_north_all'  			& `north_40'`stars_north_40'  			 & `north_10'`stars_north_10' 				& `north_5'`stars_north_5' 				& `north_rd'`stars_north_rd'      		\\
tex 			                			& (`se_north_all') 							& (`se_north_40') 						 & (`se_north_10') 							& (`se_north_5')  						& (`se_north_rd') 						\\
tex Northeast 			         			& `northeast_all'`stars_northeast_all'  	& `northeast_40'`stars_northeast_40' 	 & `northeast_10'`stars_northeast_10' 		& `northeast_5'`stars_northeast_5' 		& `northeast_rd'`stars_northeast_rd'    \\
tex 			                			& (`se_northeast_all') 						& (`se_northeast_40') 					 & (`se_northeast_10') 						& (`se_northeast_5')  					& (`se_northeast_rd') 					\\
tex South 			         				& `south_all'`stars_south_all'  			& `south_40'`stars_south_40' 			 & `south_10'`stars_south_10' 				& `south_5'`stars_south_5' 				& `south_rd'`stars_south_rd'      		\\
tex 			                			& (`se_south_all') 							& (`se_south_40') 						 & (`se_south_10') 							& (`se_south_5')  						& (`se_south_rd') 						\\
tex Southeast 			     				& `southeast_all'`stars_southeast_all' 		& `southeast_40'`stars_southeast_40' 	 & `southeast_10'`stars_southeast_10' 		& `southeast_5'`stars_southeast_5' 		& `southeast_rd'`stars_southeast_rd'    \\
tex 			                			& (`se_southeast_all') 						& (`se_southeast_40') 					 & (`se_southeast_10') 						& (`se_southeast_5')  					& (`se_southeast_rd') 					\\	
tex Midwest 			      				& `midwest_all'`stars_midwest_all'			& `midwest_40'`stars_midwest_40' 		 & `midwest_10'`stars_midwest_10' 			& `midwest_5'`stars_midwest_5' 			& `midwest_rd'`stars_midwest_rd'      	\\
tex 										& (`se_midwest_all') 						& (`se_midwest_40') 					 & (`se_midwest_10') 						& (`se_midwest_5')  					& (`se_midwest_rd') 					\\	

tex \midrule
tex \multicolumn{6}{l}{Other covariates} \\
tex \midrule

tex log(Bolsa Familia households) $\times 100$       	& `ln_bf_all'`stars_ln_bf_all' 	& `ln_bf_40'`stars_ln_bf_40'  			 & `ln_bf_10'`stars_ln_bf_10' 				& `ln_bf_5'`stars_ln_bf_5' 				& `ln_bf_rd'`stars_ln_bf_rd'      			\\
tex 			               				& (`se_ln_bf_all') 							& (`se_ln_bf_40') 						 & (`se_ln_bf_10') 							& (`se_ln_bf_5')  						& (`se_ln_bf_rd') 							\\

tex log(Bolsa Familia receipts) $\times 100$ & `ln_bf_value_all'`stars_ln_bf_value_all' & `ln_bf_value_40'`stars_ln_bf_value_40' & `ln_bf_value_10'`stars_ln_bf_value_10' 	& `ln_bf_value_5'`stars_ln_bf_value_5' 	& `ln_bf_value_rd'`stars_ln_bf_value_rd'    \\
tex 					        			& (`se_ln_bf_value_all') 					& (`se_ln_bf_value_40') 				 & (`se_ln_bf_value_10') 					& (`se_ln_bf_value_5')  				& (`se_ln_bf_value_rd') 					\\

tex Authorized amendments       			& `amend_auth_share2y_all' 					& `amend_auth_share2y_40'  				 & `amend_auth_share2y_10' 					& `amend_auth_share2y_5' 				& `amend_auth_share2y_rd'      				\\
tex 			               				& (`se_amend_auth_share2y_all') 			& (`se_amend_auth_share2y_40') 			 & (`se_amend_auth_share2y_10') 			& (`se_amend_auth_share2y_5') 			& (`se_amend_auth_share2y_rd') 				\\

tex Executed amendments				      	& `amend_exec_share2y_all' 					& `amend_exec_share2y_40'  				 & `amend_exec_share2y_10' 					& `amend_exec_share2y_5'				& `amend_exec_share2y_rd'					\\
tex 			               				& (`se_amend_exec_share2y_all') 			& (`se_amend_exec_share2y_40') 			& (`se_amend_exec_share2y_10') 				& (`se_amend_exec_share2y_5')  			& (`se_amend_exec_share2y_rd') 				\\
	
tex \bottomrule
tex Observations (all)     					& `n_all' 									& `n_40' 								 & `n_10' 									& `n_5' 								& `n_rd'      								\\
tex Observations (effective)				& `n_all'									& `n_40' 								 & `n_10' 									& `n_5' 								& `eff_rd' 									\\
tex \bottomrule
tex \end{tabularx}}
	 
	 
texdoc close
